package com.gitee.gen.jpa;

import cn.shoukaiseki.reddragon.common.domain.BaseSO;
import cn.shoukaiseki.reddragon.common.exception.RedDragonMsgException;
import cn.shoukaiseki.reddragon.common.utils.MybatisUtils;
import cn.shoukaiseki.reddragon.common.utils.StringUtils;
import com.mybatisflex.core.constant.SqlOperator;
import com.mybatisflex.core.query.QueryWrapper;
import com.mybatisflex.core.query.SqlOperators;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

import static cn.shoukaiseki.reddragon.common.utils.ListUtils.newArrayList;

public class SksJpaParser {

    public static final String[] DEFAULT_IGNORE_QUERY_PARAM_NAME={"showVirtualBitFilter"};

    //忽略的查询参数名
    private List<String> ignoreQueryParamName= newArrayList(DEFAULT_IGNORE_QUERY_PARAM_NAME);

    private BaseSO baseSO;

    public SksJpaParser(BaseSO baseSO){
       this.baseSO=baseSO;
    }

    public SksJpaParser setIgnoreQueryParamName(String... ignoreQueryParamName) {
        return setIgnoreQueryParamName(newArrayList(ignoreQueryParamName));
    }

    public SksJpaParser setIgnoreQueryParamName(List<String> ignoreQueryParamName) {
        this.ignoreQueryParamName = ignoreQueryParamName;
        if(this.ignoreQueryParamName==null){
            this.ignoreQueryParamName=newArrayList(DEFAULT_IGNORE_QUERY_PARAM_NAME);
        }

        for (String tmp : DEFAULT_IGNORE_QUERY_PARAM_NAME) {
            if(!this.ignoreQueryParamName.contains(tmp)){
                this.ignoreQueryParamName.add(tmp);
            }
        }
        return this;
    }

    /**
     * sksJpaParams 参数解析成 QueryWrapper
     */
    public QueryWrapper parserQueryWrapper(){
        Map<String, Object> queryParams = baseSO.getSksJpaParams();
        QueryWrapper queryWrapper = QueryWrapper.create();
        if (queryParams==null) {
            return queryWrapper;
        }
        String jpaName="";
        String columnName="";
        for (String paramName : queryParams.keySet()) {
            if (StringUtils.isEmpty(paramName)) {
                continue;
            }
            Object value = queryParams.get(paramName);
            MybatisUtils.escapeTableOrColumnSql(paramName);
            //忽略
            if (ignoreQueryParamName.contains(paramName)) {
                continue;
            }
            if(!MybatisUtils.isValidTableOrColumnSql(paramName)){
                throw new RedDragonMsgException("八级警报:非法的查询过滤属性,paramName={}").format(paramName);
            }
//            sql = mybatisParser(paramName, value);
            jpaName="Customize";
            if (paramName.endsWith(jpaName)) {
                //自定义的忽略
                continue;
            }
            if(value==null){
                continue;
            }
            if(value instanceof String&&StringUtils.isEmpty(value.toString())){
                continue;
            }
            jpaName="IsNotNull";
            if (paramName.endsWith(jpaName)) {
                //为1时启用过滤
                if(!StringUtils.equalsStringOne(value.toString())){
                    continue;
                }
                columnName = formatColumnName(paramName,jpaName);
                queryWrapper.isNotNull(columnName);
                continue;
            }
            jpaName="IsNotEmpty";
            if (paramName.endsWith(jpaName)) {
                //为1时启用过滤
                if(!StringUtils.equalsStringOne(value.toString())){
                    continue;
                }
                    columnName = formatColumnName(paramName,jpaName);
                queryWrapper.ne(columnName,"");
                continue;
            }

            jpaName="IsEmpty";
            if (paramName.endsWith(jpaName)) {
                //为1时启用过滤
                if(!StringUtils.equalsStringOne(value.toString())){
                    continue;
                }
                columnName = formatColumnName(paramName,jpaName);
                queryWrapper.eq(columnName,"");
                continue;
            }
        }
        return queryWrapper;
    }

    //包含
    public String formatColumnName(String queryName,String jpaName){
        String columnName=queryName.substring(0,queryName.lastIndexOf(jpaName));
        return columnName;
    }

}
